package com.hfzy.ihk.web.userCenter.biz;

import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.web.oauth.client.utils.UserUtil;
import com.hfzy.ihk.web.userCenter.mapper.UserHasPrivMapper;
import com.hfzy.ihk.web.userCenter.mapper.UserHasResourceMapper;
import com.hfzy.ihk.web.userCenter.mapper.UserHasRoleMapper;
import com.hfzy.ihk.web.userCenter.mapper.UserMapper;
import com.hfzy.ihk.web.userCenter.vo.UserInfoDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by YANFA on 2019/2/14.
 */
@Component
@Slf4j
public class UserBiz {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserHasRoleMapper userHasRoleMapper;

    @Autowired
    UserHasPrivMapper userHasPrivMapper;



    @Autowired
    UserHasResourceMapper userHasResourceMapper;

    @Autowired
    PasswordEncoder passwordEncoder;
    /**
     * 保存用户
     * @param userDto
     */
    @Transactional
    public void saveUser(UserInfoDto userDto){
        //判断该用户登录名是否存在
        if(userDto==null||userDto.getUsername()==null)
            throw new IllegalArgumentException("数据异常");

        if(userDto.getId()!=null){//更新
            updateUser(userDto);
        }else{//插入
            insertUser(userDto);
        }

    }


    /**
     * 插入
     * @param userInfo
     */
    public void insertUser(UserInfoDto userInfo){

        UserInfo u = userMapper.findUserByUserName(userInfo.getUsername());
        if(u!=null){
            throw new IllegalArgumentException("用户名已存在");
        }
        UserInfo loginUser = UserUtil.getLoginUser();
        userInfo.setEnabled(true);
        if(loginUser!=null){
            userInfo.setCreateTime(new Date());
            userInfo.setCreateUserId(userInfo.getId());
        }
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));

        userMapper.insert(userInfo);

        //保存用户-角色关系
        if(userInfo.getRoleIds()!=null)
             userHasRoleMapper.saveUserHasRole(userInfo.getId().longValue(),userInfo.getRoleIds());

        //保存用户-权限关系
        if(userInfo.getPrivIds()!=null)
            userHasPrivMapper.saveUserHasPriv(userInfo.getId().longValue(),userInfo.getPrivIds());

        //保存用户-菜单关系
        if(userInfo.getResourceIds()!=null)
            userHasResourceMapper.saveUserHasResource(userInfo.getId().longValue(),userInfo.getResourceIds());

    }


    /**
     * 更新
     * @param userInfo
     */
    public void updateUser(UserInfoDto userInfo){
        UserInfo u = userMapper.findUserByUserName(userInfo.getUsername());
        if(u!=null&&u.getId()!=userInfo.getId()){
            throw new IllegalArgumentException("用户名已存在");
        }
        UserInfo myu = userMapper.selectById(userInfo.getId());

        UserInfo loginUser = UserUtil.getLoginUser();
        if(loginUser!=null){
            userInfo.setModTime(new Date());
            userInfo.setModUserId(loginUser.getId());
        }
        userInfo.setEnabled(myu.getEnabled());
        userMapper.updateById(userInfo);

        //先删除关系再生成
        userHasRoleMapper.deleteUserHasRoleByUserId(userInfo.getId().longValue());
        if(userInfo.getRoleIds()!=null) {

            userHasRoleMapper.saveUserHasRole(userInfo.getId().longValue(), userInfo.getRoleIds());
        }

        userHasPrivMapper.deleteUserHasPrivByUserId(userInfo.getId().longValue());
        if(userInfo.getPrivIds()!=null) {
            userHasPrivMapper.saveUserHasPriv(userInfo.getId().longValue(), userInfo.getPrivIds());
        }

        userHasResourceMapper.deleteUserHasResourceByUserId(userInfo.getId().longValue());
        if(userInfo.getResourceIds()!=null) {
            userHasResourceMapper.saveUserHasResource(userInfo.getId().longValue(), userInfo.getResourceIds());

        }


    }


    public String changePassword(String username, String oldPassword, String newPassword) {
        UserInfo u = userMapper.findUserByUserName(username);
        if (u == null) {
            return "用户不存在";
        }

        if (!passwordEncoder.matches(oldPassword, u.getPassword())) {
            return "旧密码错误";
        }
        userMapper.changePassword(u.getId().longValue(), passwordEncoder.encode(newPassword));
        log.debug("修改{}的密码", username);
        return "修改成功";

    }

    public List<UserInfo> querySimpleUserInfo(List<Integer> userId){
        return userMapper.querySimpleUserInfo(userId);
    };


}
